<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm from">
    <el-form-item label="用户名" prop="name">
      <el-input v-model="ruleForm.name"/>
    </el-form-item>
    <el-form-item label="手机号码" prop="mobileNumber">
      <el-input v-model.number="ruleForm.mobileNumber"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-col :span="8">
        <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
      </el-col>
      <el-col :span="8" style="text-align: center">
        <el-button @click="$router.push('/sign-in')">登录</el-button>
      </el-col>
      <el-col :span="8">
        <el-button style="float: right" @click="resetForm('ruleForm')">重置</el-button>
      </el-col>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: "SignUp",
  data() {
    return {
      ruleForm: {
        name: '',
        password: '',
        checkPass: '',
        mobileNumber: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 32, message: '长度应为 6 到 32 个字符', trigger: 'blur'},
          {
            validator: (rule, value, callback) => {
              if (this.ruleForm.checkPass !== '') {
                this.$refs.ruleForm.validateField('checkPass');
              }
              callback();
            }, trigger: 'blur'
          }
        ],
        checkPass: [
          {required: true, message: '请再次输入密码', trigger: 'blur'},
          {
            validator: (rule, value, callback) => {
              if (value !== this.ruleForm.password) {
                callback(new Error('两次输入密码不一致!'));
              } else {
                callback();
              }
            }, trigger: 'blur'
          }
        ],
        mobileNumber: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
          {
            validator: (rule, value, callback) => {
              if (!Number.isInteger(value)) {
                callback(new Error('手机号码必须为数字值'));
              } else {
                // if (value.length !== 11) {
                //     callback(new Error('号码有误'));
                // } else {
                this.$axios.get(this.$serverUrl
                    + '/user/check?mobileNumber=' + this.ruleForm.mobileNumber)
                    .then(response => {
                      console.log(response.data)
                      if (response.data) {
                        console.log(response.data)
                        callback()
                      } else {
                        callback(new Error('该号码已被注册'))
                      }
                    })
                // }
              }
            }, trigger: 'blur'
          }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const tmp0 = this.ruleForm.password
          const tmp1 = this.ruleForm.mobileNumber
          this.ruleForm.password = this.$md5(tmp0 + tmp1)
          this.$axios.post(this.$serverUrl
              + '/user/signUp', this.ruleForm)
              .then(response => {
                console.log(response)
                this.$router.push('/sign-in')
                this.$message({
                  showClose: true,
                  message: '注册成功! 请登录!',
                  type: 'success'
                });
              })
              .catch(error => {
                console.log(error)
                this.$message({
                  showClose: true,
                  message: '输入有误!',
                  type: 'error'
                });
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.from {
  max-width: 500px;
  margin: 50px auto;
}
</style>